package com.psm.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.jfinal.aop.Before;
import com.jfinal.render.JsonRender;
import com.psm.interceptor.IndexInterceptor;
import com.psm.model.WXTalent;
import com.psm.model.WXTalentDetail;

@Before(IndexInterceptor.class)
public class WXTalentController extends BaseController<WXTalent>{
	
	public Logger log=Logger.getLogger(getClass());
	public static JSONArray jsonStrs = new JSONArray();
	public static List<WXTalentDetail> list = new ArrayList<WXTalentDetail>();
	
	
	/**
	 * 主页
	 */
    public void index(){
        render("index.html");
    }
    /**
     * 单图文
     */
    public void addone(){
    	render("addone.html");
    }
    /**
     * 多图文
     */
    public void addmore(){
    	jsonStrs = new JSONArray();
    	render("addmore.html");
    }
    
    /**
     * 保存信息
     */
    public void saveone(){
    	/**
    	 * 获取数据
    	 */
    	 String id = getPara("talent_detail_id","");
    	 String tid = getPara("talent_id","");
    	 String title = getPara("title","");
         String pic_url = getPara("pic_url","");
         String description = getPara("description","");
         String url = getPara("url","");
         String content = getPara("content","");
         
         System.out.println("================================= save one");
         /**
          * 新建对象
          */
         WXTalent wx = null;
         WXTalentDetail wtd = null;
         if("".equals(id)){
             wx = new WXTalent();
             wtd = new WXTalentDetail();
             wx.set("msg_type","707");
             wx.set("status","0");
             wx.save();
             System.out.println( wx.get("id").toString()+"==================================");
             wtd.set("talent_id",wx.get("id").toString());
             wtd.set("title",title);
             wtd.set("pic_url",pic_url);
             wtd.set("content",content);
             wtd.set("description",description);
             wtd.set("url",url);
             wtd.set("status","0");
             wtd.save();
         }else{
             wx = WXTalent.wx.findById(id);
         }
         
         //详细
         
         
         
         
         

//         /**
//          * 更新数据
//          */
//         if("".equals(id)){
//        	 wx.set("create_time", System.currentTimeMillis()/1000);
//             wx.save();
//         }else{
//        	 wx.set("modify_time", System.currentTimeMillis()/1000);
//             wx.update();
//         }
         this.rendJson_(1, "操作成功");
    }

    public void load(){
    	
    	WXTalentDetail wtd = new WXTalentDetail();
    	if(list.size()==0){
    		wtd.set("id",1);
        	wtd.set("title","标题");
            wtd.set("pic_url","");
            wtd.set("content","");
            wtd.set("description","摘要");
            wtd.set("url","");
            wtd.set("status","0");
            list.add(wtd);
        	
//        	jsonStrs.add(0, wtd);
        	
    	}
    	
//    	JSONArray jsonStrs1 =jsonStrs;
//    	List<WXTalentDetail> list1 = list;
    	setAttr("state", 1);
        setAttr("value", list);
//    	this.rendJson_(1, "操作成功");
        render(new JsonRender().forIE());
    }

    public void add_detail(){
    	/**
    	 * 获取数据
    	 */
//    	System.out.println("jsonstr size"+jsonStrs.size());
//    	JSONArray jsonStrs1 =jsonStrs;
//    	List<WXTalentDetail> list1 = list;
    	String id = getPara("talent_detail_id","");
    	if(!"".equals(id)&&id!=null){
    		String tid = getPara("talent_id","");
       	 	String title = getPara("title","");
            String pic_url = getPara("pic_url","");
            String description = getPara("description","");
            String url = getPara("url","");
            String content = getPara("content","");
            
//            WXTalentDetail owtDetail = (WXTalentDetail)jsonStrs.get(Integer.parseInt(id)-1);
            WXTalentDetail owtDetail = list.get(Integer.parseInt(id)-1);
            
            owtDetail.set("id",id);
            owtDetail.set("title",title);
            owtDetail.set("pic_url",pic_url);
            owtDetail.set("content",content);
            owtDetail.set("description",description);
            owtDetail.set("url",url);
            owtDetail.set("status","0");
            
//            jsonStrs.add(Integer.parseInt(id)-1, owtDetail);
            list.set(Integer.parseInt(id)-1, owtDetail);
//            list1 = list;
//            jsonStrs1 =jsonStrs;
            
//            System.out.println("jsonstr size"+jsonStrs.size());
//            System.out.println(jsonStrs.get(Integer.parseInt(id)-1).toString()+"=============");
            
            WXTalentDetail wtd = new WXTalentDetail();
    		wtd.set("id",list.size()+1);
    		wtd.set("title","标题");
    	    wtd.set("pic_url","");
    	    wtd.set("content","");
    	    wtd.set("description","摘要");
    	    wtd.set("url","");
    	    wtd.set("status","0");
    	    list.add(wtd);
    	    System.out.println(list.size()+"=============");
    	}else{
    		WXTalentDetail wtd = new WXTalentDetail();
    		wtd.set("id",list.size()+1);
    		wtd.set("title","标题");
    	    wtd.set("pic_url","");
    	    wtd.set("content","");
    	    wtd.set("description","摘要");
    	    wtd.set("url","");
    	    wtd.set("status","0");
//    	    jsonStrs.add(jsonStrs.size()-1+1, wtd);
    	    list.add(wtd);
    	}
    	
//    	jsonStrs1 =jsonStrs;
		setAttr("state", 1);
	    setAttr("value", list);
	    render(new JsonRender().forIE());
    }
    
    public void add(){
    	/**
    	 * 获取数据
    	 */
    	String id = getPara("talent_detail_id","");
    	if(!"".equals(id)&&id!=null){
    		String tid = getPara("talent_id","");
       	 	String title = getPara("title","");
            String pic_url = getPara("pic_url","");
            String description = getPara("description","");
            String url = getPara("url","");
            String content = getPara("content","");
            
            WXTalentDetail owtDetail = list.get(Integer.parseInt(id)-1);
            
            owtDetail.set("id",id);
            owtDetail.set("title",title);
            owtDetail.set("pic_url",pic_url);
            owtDetail.set("content",content);
            owtDetail.set("description",description);
            owtDetail.set("url",url);
            owtDetail.set("status","0");
            
            list.set(Integer.parseInt(id)-1, owtDetail);
            
            /**
             * 新建对象
             */
            WXTalent wx = new WXTalent();
            
            wx.set("msg_type","708");
            wx.set("status","0");
            wx.save();
            
            String wtid = wx.get("id").toString();
            WXTalentDetail wtd = null;
            
            for (int i = 0; i < list.size(); i++) {
            	
            	wtd = new WXTalentDetail();
                
                wtd.set("talent_id",wtid);
                wtd.set("title",list.get(i).get("title"));
                wtd.set("pic_url",list.get(i).get("pic_url"));
                wtd.set("content",list.get(i).get("content"));
                wtd.set("description",list.get(i).get("description"));
                wtd.set("url",list.get(i).get("url"));
                wtd.set("status",list.get(i).get("status"));
                wtd.save();
			}
                
    	}else{
    		
    	}
    	
    	this.rendJson_(1, "操作成功");
    }
}
